package com.stark.jarvis.cipher.core;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * I/O工具
 *
 * @author <a href="mailto:mengbin@hotmail.com">Ben</a>
 * @since 1.0.0, 2025/8/14
 */
public class IOUtils {

    private static final int DEFAULT_BUFFER_SIZE = 8192;

    /**
     * 转换输入流为字节数组
     *
     * @param inputStream 输入流
     * @return 字节数组
     * @throws IOException 读取字节失败、关闭流失败等
     */
    public static byte[] toByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int nRead;
        byte[] data = new byte[DEFAULT_BUFFER_SIZE];
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        return buffer.toByteArray();
    }

    /**
     * 转换输入流为字符串
     *
     * @param inputStream 输入流
     * @return UTF-8编码的字符串
     * @throws IOException 读取字节失败、关闭流失败等
     */
    public static String toString(InputStream inputStream) throws IOException {
        return new String(toByteArray(inputStream), StandardCharsets.UTF_8);
    }

    /**
     * 从文件路径中读取字符串
     *
     * @param path 文件路径
     * @return UTF-8编码的字符串
     */
    public static String loadStringFromPath(String path) {
        try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
            return toString(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("读取文件失败", e);
        }
    }

}
